package cn.iocoder.yudao.module.system.controller.admin.workhanging;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.workhanging.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.workhanging.WorkHangingDO;
import cn.iocoder.yudao.module.system.service.workhanging.WorkHangingService;

@Tag(name = "管理后台 - 下挂记录")
@RestController
@RequestMapping("/system/work-hanging")
@Validated
public class WorkHangingController {

    @Resource
    private WorkHangingService workHangingService;

    @PostMapping("/create")
    @Operation(summary = "创建下挂记录")
    @PreAuthorize("@ss.hasPermission('system:work-hanging:create')")
    public CommonResult<Integer> createWorkHanging(@Valid @RequestBody WorkHangingSaveReqVO createReqVO) {
        return success(workHangingService.createWorkHanging(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新下挂记录")
    @PreAuthorize("@ss.hasPermission('system:work-hanging:update')")
    public CommonResult<Boolean> updateWorkHanging(@Valid @RequestBody WorkHangingSaveReqVO updateReqVO) {
        workHangingService.updateWorkHanging(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除下挂记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:work-hanging:delete')")
    public CommonResult<Boolean> deleteWorkHanging(@RequestParam("id") Integer id) {
        workHangingService.deleteWorkHanging(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除下挂记录")
                @PreAuthorize("@ss.hasPermission('system:work-hanging:delete')")
    public CommonResult<Boolean> deleteWorkHangingList(@RequestParam("ids") List<Integer> ids) {
        workHangingService.deleteWorkHangingListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得下挂记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:work-hanging:query')")
    public CommonResult<WorkHangingRespVO> getWorkHanging(@RequestParam("id") Integer id) {
        WorkHangingDO workHanging = workHangingService.getWorkHanging(id);
        return success(BeanUtils.toBean(workHanging, WorkHangingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得下挂记录分页")
    @PreAuthorize("@ss.hasPermission('system:work-hanging:query')")
    public CommonResult<PageResult<WorkHangingRespVO>> getWorkHangingPage(@Valid WorkHangingPageReqVO pageReqVO) {
        PageResult<WorkHangingDO> pageResult = workHangingService.getWorkHangingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WorkHangingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出下挂记录 Excel")
    @PreAuthorize("@ss.hasPermission('system:work-hanging:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWorkHangingExcel(@Valid WorkHangingPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WorkHangingDO> list = workHangingService.getWorkHangingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "下挂记录.xls", "数据", WorkHangingRespVO.class,
                        BeanUtils.toBean(list, WorkHangingRespVO.class));
    }

}